package com.stu.action.service;

import java.io.IOException; 
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.stu.action.base.BaseAction;
import com.stu.entity.permission.User;
import com.stu.entity.service.TechnologyBlog;
import com.stu.facade.service.TechnologyBlogFacade;
import com.stu.service.service.ITechnologyBlogService;
import com.stu.utils.CommonUtils;
import com.stu.utils.PageBean;

@Controller
@Scope("prototype")
public class TechnologyBlogAction extends BaseAction<TechnologyBlog, ITechnologyBlogService> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	/**
	 * 保存博客 
	 * @author lijian
	 *
	 */
	public void saveBlog(){
		JSONObject json=new JSONObject();
		User user=CommonUtils.getCurrentUser("currentUser");
		if(null!=user){
			model.setUser(user);
			Serializable s=super.save();
			if(StringUtils.isNotBlank(s+"")){//保存成功
				model.setUuid(s+"");
				jedisUtils.zadd("blogs", System.currentTimeMillis(), model);
				json.put("success", true);
				logger.info("save blog success: "+s);
				logger.info("currentUser : "+CommonUtils.getSessionAttribute("currentUser"));
			}else{//保存失败 
				json.put("success", false);
				logger.warn("save blog fail : "+s);
				logger.info("currentUser : "+CommonUtils.getSessionAttribute("currentUser"));
			}
			json.put("data", "");
		}else{ 
			json.put("success", false);
		}
		try { 
			CommonUtils.writeJson(json.toString()); 
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 使用 ajax 方式初始化blog_list 页面 blog列表
	 * @author lijian
	 *
	 */
	public void list(){
		String fr=getRequest().getParameter("fr");
		List<TechnologyBlog>blogList=null;
		PageBean pageBean=null;
		if(StringUtils.isNotBlank(fr)){
			try {
				blogList=jedisUtils.zrevrange("blogs", 0, System.currentTimeMillis(), rows);
			} catch (Exception e) {
				String hql="from TechnologyBlog order by createTime desc";
				String title=model.getTitle();
				Map<String,Object>params=new HashMap<String,Object>();
				if(StringUtils.isNotBlank(title)){
					hql="from TechnologyBlog where title like :title order by createTime desc";
					params.put("title", "%"+title+"%"); 
				}
				blogList=super.findByHql(hql, params);
				
				pageBean=new PageBean(page, getAccount(), rows);
			}
			if(blogList==null||blogList.size()<rows){//如果键不存在，不会返回错误，但是没有数据，就需要自己查询
				String hql="from TechnologyBlog order by createTime desc";
				String title=model.getTitle();
				Map<String,Object>params=new HashMap<String,Object>();
				if(StringUtils.isNotBlank(title)){
					hql="from TechnologyBlog where title like :title order by createTime desc";
					params.put("title", "%"+title+"%"); 
				}
				blogList=super.findByHql(hql, params);
				
				pageBean=new PageBean(page, getAccount(), rows);
			}
		}else{
			String hql="from TechnologyBlog order by createTime desc";
			String title=model.getTitle();
			Map<String,Object>params=new HashMap<String,Object>();
			if(StringUtils.isNotBlank(title)){
				hql="from TechnologyBlog where title like :title order by createTime desc";
				params.put("title", "%"+title+"%"); 
			}
			blogList=super.findByHql(hql, params);
			
			pageBean=new PageBean(page, getAccount(), rows);
		}
		List<TechnologyBlogFacade> facadeList=null;
		if(null!=blogList){
			facadeList=TechnologyBlogFacade.getFacadeList(blogList);
		}
		
		JSONObject json=new JSONObject();
		json.put("success", true);
		json.put("data", facadeList); 
		json.put("pageBean", pageBean); 
		try {  
			CommonUtils.writeJson(json.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获取 blog 详情
	 * @author lijian
	 *
	 * @return
	 */
	public String getById(){
		String id=getRequest().getParameter("id");
		if(StringUtils.isNotBlank(id)){
			TechnologyBlog blog=super.findById(id);
			if(null!=blog){ 
				CommonUtils.getValueStack().getContext().put("blog", blog);
				return "blogDetails";
			}
		}
		return "ERROR";
	}
	
	/**
	 * 查询我的 blog
	 * @author lijian
	 *
	 */
	public void own(){
		JSONObject json=new JSONObject();
		User user=CommonUtils.getCurrentUser("currentUser");
		if(null!=user){
			String hql="from TechnologyBlog where 1=1 ";
			Map<String,Object>params=new HashMap<String,Object>();
			String title=getRequest().getParameter("title");
			
			if(StringUtils.isNotBlank(title)){  
				hql+=" and title like :title ";  
				params.put("title", "%"+title+"%");
			}
			if(!User.isAdmin(user)){ 
				hql+=" and user=:curuser";
				params.put("curuser", user);
			}
			hql+=" order by createTime desc ";
			List<TechnologyBlog>blogList=super.findByHql(hql, params);
			List<TechnologyBlogFacade> facadeList=null;
			if(null!=blogList){
				facadeList=TechnologyBlogFacade.getFacadeList(blogList);
			} 
			PageBean pageBean=new PageBean(page, getMyBlogAccount(), rows);
			
			json.put("success", true);
			json.put("data", facadeList); 
			json.put("pageBean", pageBean); 
		}else{ 
			json.put("success", false);
		}
		try { 
			CommonUtils.writeJson(json.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 博客 修改
	 * @author lijian
	 *
	 * @return 
	 */
	public String updateById(){
		TechnologyBlog blog=super.findById(getRequest().getParameter("id"));
		CommonUtils.getValueStack().push(blog);
		return "editBlog";
	}
	
	public void updateBlog(){
		boolean flag=super.update();
		JSONObject json=new JSONObject();
		json.put("success", flag);
		try {
			CommonUtils.writeJson(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void deleteBlogsByIds(){
		boolean flag=super.deleteByIds();
		JSONObject json=new JSONObject();
		json.put("success", flag);
		try { 
			CommonUtils.writeJson(json.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private int getMyBlogAccount(){
		String hql="select count(*) from TechnologyBlog where 1=1 ";
		String title=getRequest().getParameter("title");
		Map<String,Object>params=new HashMap<String,Object>();
		if(StringUtils.isNotBlank(title)){
			hql+="and title like :title ";
			params.put("title", "%"+title+"%");
			getRequest().setAttribute("title", title);
		}
		User user=CommonUtils.getCurrentUser("currentUser");
		if(!User.isAdmin(user)){
			hql+=" and user=:curuser";
			params.put("curuser", user); 
		}
		return super.getAccount(hql, params);
	}
	
	/** 
	 * 根据查询条件获取记录数
	 * @author lijian
	 * @return
	 */
	private int getAccount(){
		String hql="select count(*) from TechnologyBlog";
		String title=getRequest().getParameter("title");
		Map<String,Object>params=new HashMap<String,Object>();
		if(StringUtils.isNotBlank(title)){
			hql="select count(*) from TechnologyBlog where title like :title ";
			params.put("title", "%"+title+"%");
			getRequest().setAttribute("title", title);
		}
		return super.getAccount(hql, params);
	}
	
}
